package com.baixun.gasadmin.controller;


import com.baixun.gascommon.utils.R;
import com.baixun.gasentity.entity.SysDepartment;
import com.baixun.gasentity.entity.SysDepartmentRoles;
import com.baixun.gasentity.entity.SysDepartmentUserRoles;
import com.baixun.gassystem.service.BackOfficeUserInfoService;
import com.baixun.gassystem.service.SysDepartmentRoleService;
import com.baixun.gassystem.service.SysDepartmentUserRolesService;
import com.baixun.gassystem.service.SysVideoMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;


/**
 * 用户管理后台
 */
@RestController
@RequestMapping("/roles")
@Api(tags = "后台管理系统接口")
public class SysRoleController {

    @Autowired
    private SysDepartmentRoleService roleService;


    @Autowired
    private BackOfficeUserInfoService backOfficeUserInfoService;

    @Autowired
    public SysVideoMessageService sysVideoMessageService;

    @Autowired
    private SysDepartmentUserRolesService sysDepartmentUserRolesService;

    /**
     * -----------------------------------------------------------------------以下是角色管理模块----------------------------------------------------------------------------------
     */

    @ApiOperation(value = "获取所有角色", notes = "返回系统中的所有角色")
    @GetMapping
    @PreAuthorize("hasAuthority('VIEW_ALL_PAGES')")
    public R<List<SysDepartmentRoles>> getAllRoles() {
        return R.success(roleService.getAllRoles());
    }

    @ApiOperation(value = "根据角色名称模糊查询", notes = "根据输入的角色名称进行模糊查询并返回匹配的角色列表")
    @GetMapping("/search")
    @PreAuthorize("hasAuthority('VIEW_ALL_PAGES')")
    public R<List<SysDepartmentRoles>> getRoleByName(@ApiParam(value = "角色名称", required = true) @RequestParam String name) {
        return R.success(Collections.singletonList(roleService.getRoleByName(name)));
    }

    @ApiOperation(value = "创建新角色", notes = "创建一个新的角色并保存到数据库")
    @PostMapping
    @PreAuthorize("hasAuthority('VIEW_ALL_PAGES')")
    public R<Void> createRole(@ApiParam(value = "新角色对象", required = true) @RequestBody SysDepartmentRoles role) {
        roleService.createRole(role);
        return R.success(null);
    }

    @ApiOperation(value = "更新角色", notes = "更新现有角色的信息")
    @PutMapping
    @PreAuthorize("hasAuthority('VIEW_ALL_PAGES')")
    public R<Void> updateRole(@ApiParam(value = "更新后的角色对象", required = true) @RequestBody SysDepartmentRoles role) {
        roleService.updateRole(role);
        return R.success(null);
    }

    @ApiOperation(value = "删除角色", notes = "根据角色ID删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('VIEW_ALL_PAGES')")
    public R<Void> deleteRole(@ApiParam(value = "角色ID", required = true) @PathVariable Long id) {
        roleService.deleteRole(id);
        return R.success(null);
    }

    @ApiOperation(value = "分配权限", notes = "为指定角色分配权限")
    @PostMapping("/{id}/permissions")
    @PreAuthorize("hasAuthority('VIEW_ALL_PAGES')")
    public R<Void> assignPermissions(
            @ApiParam(value = "角色ID", required = true) @PathVariable Long id,
            @ApiParam(value = "权限ID列表", required = true) @RequestBody List<Long> permissionIds) {
        roleService.assignPermissions(id, permissionIds);
        return R.success(null);
    }

    /**
     * -----------------------------------------------------------------------以下是用户管理模块----------------------------------------------------------------------------------
     */

//@RestController
//@RequestMapping("/permissions")
//public class PermissionController {
//
//    @ApiOperation(value = "查询所有权限列表", notes = "根据用户ID和部门ID查询所有权限")
//    @GetMapping("/allUserTree")
//    public ResponseEntity<List<SysDataPermission>> getAllPermissions(
//            @ApiParam(value = "用户ID", required = true) @RequestParam Long userId,
//            @ApiParam(value = "部门ID", required = true) @RequestParam Long departmentId) {
//        List<SysDataPermission> permissions = permissionService.getAllPermissions(userId, departmentId);
//        return ResponseEntity.ok(permissions);
//    }
//
//    @ApiOperation(value = "修改当前权限", notes = "根据用户ID和部门ID更新权限")
//    @PostMapping("/update")
//    public ResponseEntity<String> updatePermissions(
//            @ApiParam(value = "用户ID", required = true) @RequestParam Long userId,
//            @ApiParam(value = "部门ID", required = true) @RequestParam Long departmentId,
//            @ApiParam(value = "需要添加和移除的节点ID列表", required = true) @RequestBody Map<String, List<Long>> nodes) {
//        List<Long> nodesToAdd = nodes.get("nodesToAdd");
//        List<Long> nodesToRemove = nodes.get("nodesToRemove");
//
//        permissionService.updatePermissions(userId, departmentId, nodesToAdd, nodesToRemove);
//        return ResponseEntity.ok("Permissions updated successfully");
//    }
//}



    @ApiOperation(value = "获取所有用户信息", notes = "返回系统中所有用户的信息列表")
    @GetMapping("/all")
    @PreAuthorize("hasAuthority('VIEW_ALL_PAGES')")
    public R<List<String>> getAllUserInfo() throws Exception {
        return R.success(backOfficeUserInfoService.getAllUserInfo());
    }

    @ApiOperation(value = "获取部门树", notes = "返回部门层级")
    @GetMapping("/dept")
    @PreAuthorize("hasAuthority('VIEW_ALL_PAGES')")
    public R<List<SysDepartment>> getDeptTree() throws Exception {
        return R.success(backOfficeUserInfoService.getDepartmentTree());
    }


    @ApiOperation(value = "分配角色", notes = "为指定用户分配角色")
    @GetMapping("/{id}/roles")
    @PreAuthorize("hasAuthority('VIEW_ALL_PAGES')")
    public R assignRole(
        @ApiParam(value = "部门用户角色信息", required = true) @RequestBody SysDepartmentUserRoles departmentUserRoles
    ) {
        return R.success(sysDepartmentUserRolesService.assignRole(departmentUserRoles));
    }

}
